import turtle as t
import random

x = 0
y = 400
def gen_points():
    """生成坐标列表"""
    points  = []
    for i in range(5):
        points.append([340,random.randint(-340, 340)])
    return points


def youbianhuanyuan():
    """
    绘制
    :return:
    """
    t.clear()
    draw_dot()
    global  index, y
    is_end = False
    for pos in points:
        t.up()
        t.goto(pos)
        t.down()
        t.dot(20, 'cyan')

        if pos[1]-20<y<pos[1]+20 and pos[0]-20< x<pos[0]+20:
            is_end = True
        pos[0]-=random.randint(10, 30)
    y = y - 10
    # 能被无整除则添加多个坐标
    if index%5==0:
        points.extend(gen_points())
        index=1
    else:
        index +=1

    if not is_end:
        t.ontimer(youbianhuanyuan, 500)
    else:
        t.up()
        t.goto(0, 100)
        t.down()
        t.write('游戏结束',True, 'center', ('Arial', 30, 'normal'))

def updown(a,b):
    global y
    y = y+20

def draw_dot():
    global y
    t.up()
    t.goto(x, y)
    t.down()
    t.dot(20, 'blue')


# 控制小球右侧每一轮出现的速度
index = 1
points = gen_points()
t.tracer(False)
youbianhuanyuan()
t.onscreenclick(updown)
t.done()
